
var express = require('express');
var mysql=require("./db");
var router = express.Router();
router.get('/', function(req, res, next) {
    res.render("register",{title: '注册'})
});

router.route('/reg').get(function (req,res) {
    res.render('register',{title:'注册'});
}).post(function (req,res) {
      var name=req.body.uname;
      var password=req.body.upwd;
      var sex=req.body.usex;
      mysql.query("SELECT * FROM  userinfo where UserName='"+name+"'",function (rows) {
          if(rows.length){
              return res.json({success:false, message:"当前用户已存在", data:null})
          }
          else
           {
               mysql.query("INSERT INTO userinfo (UserName,`Password`,Sex) VALUES('"+name+"','"+password+"','"+sex+"')",function (rows) {
                   if(rows.affectedRows>0){
                       return res.json({success:true, message:"注册成功请登录", data:null})
                   }
                   else{
                       return res.json({success:false, message:"注册失败", data:null})

                   }


               })

          }

      })

})


module.exports = router;